package com.yonyou.rabbitmq.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

@Slf4j
public class MessageUtils {

    public static String getBodyStr(Message message) {
        try {
            return new String(message.getBody(), Charset.defaultCharset().name());
        } catch (UnsupportedEncodingException e) {
            log.error("getBodyStr错误，错误原因：{}",e);
            return "";
        }
    }

    public static String getPropertiesStr(Message message) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(message.getMessageProperties());
        } catch (JsonProcessingException e) {
            log.error("getPropertiesStr错误，错误原因：{}",e);
            return "";
        }
    }
}
